package org.springblade.modules.platform.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class ModelUtils {

	public Map<String,String> modelParsing(String propertiesJson,String cmd) {
		Map<String,String> tableHeader = new HashMap<>();
		JSONArray jsonArray = JSONArray.parseArray(propertiesJson);
		for (Object object : jsonArray) {
			JSONObject object1 = (JSONObject) object;
			String id = object1.get("id").toString();
			if (id.equalsIgnoreCase(cmd)) {
				JSONObject o3 = (JSONObject) object1.get("valueType");
				if(o3.containsKey("properties")){
					JSONArray jsonArray1 = JSONArray.parseArray(o3.get("properties").toString());
					for(Object o:jsonArray1){
						JSONObject o1 = (JSONObject) o;
						String string1 = o1.get("name").toString();
						if(o1.containsKey("dataType")){
							String string = o1.get("dataType").toString();
							if(string.equalsIgnoreCase("enum")){
								JSONObject valueType = (JSONObject)(o1.get("valueType"));
								JSONArray elements = JSONArray.parseArray(valueType.get("elements").toString());
								string1 = string1 + "(";
								for(Object element:elements){
									JSONObject el = (JSONObject) element;
									String text = el.get("text").toString();
									String value = el.get("value").toString();
									string1 = string1  + text +":"+value+";";
								}
								string1 = string1 + ")" ;
							}else if(string.equalsIgnoreCase("int")){
								JSONObject valueType = (JSONObject)(o1.get("valueType"));
								String unit = valueType.get("unit").toString();
								string1 = string1 + "("+"单位:" + unit+ ")" ;
							}
						}
						tableHeader.put(o1.get("id").toString(),string1);
					}
				}else if(o3.containsKey("unit")){
					String unit = o3.get("unit").toString();
					tableHeader.put(object1.get("id").toString(),object1.get("name").toString()+ "("+"单位:" + unit+ ")");
				}else {
					tableHeader.put(object1.get("id").toString(),object1.get("name").toString());
				}
			}
		}
		return tableHeader;

	}

}




